<?php 
/**
 *+-------IMBA用户业务数据的获取操作--------+
 *+-------------------------------------------------+
 *+-------用于提供内部接口---------------+
 *+-------------------------------------------------+
 *+-@author susy.liang <liangling@ilongyuan.com.cn>-+
 *+-------------------------------------------------+
 */
namespace Api\Event;
use User\Api\UserApi;
use Api\Constract\IPosable;

class UserEvent extends BaseEvent implements IPosable {

	/**
	 *模型对象
	 */
	protected $mod;

	/**
	 *成员变量的初始化
	 */
	protected function _initialize() {
		parent::_initialize();
		$this->mod = service('Users');
	}

	/**
	 *获取用户信息
	 *@param int $uid 用户标识
	 *@param boolean $regtime 获取注册时间 true需要 false否
	 *@return array nickname:昵称， avatar:头像
	 */
	// 添加$showUsername参数  liubo 2016-07-11
	public function info($uid, $regtime = false, $ufield = '', $showUsername = false) {
		static $TUser;
		static $TAuthIcon;
		$uid = (int)$uid;
		if (!empty($TUser[$uid])) {
			return $TUser[$uid];
		}
		$field = 'id,nickname,avatar,background,gender,address,profession,stars,age,interest,desc,game,level,exp,score,voice,referral,auth_icon,status,turtial,email,email_verify,sso_channel';
 
		// $field = $ufield?$ufield:$field;
		if ($regtime){
			$field .= ',reg_time';
		}
		if ($showUsername) {
			$field .= ',username';
		}
		$user = $this->mod->where(['id' => $uid])->field($field)->find();

		// 增加判断status 避免notice
		if (!$user || empty($user['status']) || $user['status'] == -1) {
			return ['nickname' => '该用户已经被删除'];
		}
		unset($user['_id']);
		// $user['auth'] = '';
		if (!empty($user['auth_icon'])){
			if (empty($TAuthIcon[$user['auth_icon']]))
			{
				$res = event('IconPacks')->getIconById($user['auth_icon']);
				$res && $TAuthIcon[$user['auth_icon']] = field_filter($res, ['pic', 'name'], false);
			}
			$auth = $TAuthIcon[$user['auth_icon']];
			$auth && $user['auth'] = $auth;
 		}

 		$user['turtial'] = !empty($user['turtial']) ? intval($user['turtial']) : 0;

		if (empty($user['avatar'])) {
			$user['avatar'] = '';
		}
		$user['turtial'] = !empty($user['turtial']) ? intval($user['turtial']) : 0;
//C('LOCAL_DOMAIN').'/Public/Home/images/avatar.jpg';
		if (empty($user['background'])) {
			$user['background'] = '';
		}
//C('LOCAL_DOMAIN').'/Public/Home/images/background.jpg';
		uri_format($user['avatar']);
		uri_format($user['background']);

		$user['game'] = !empty($user['game']) ? explode(',', $user['game']) : array();

		// 计算经验值比例
		#TODO function
 		$next_level = $user['level'] + 1;
		$next_exp = 10 * pow(($next_level - 1), 2) + 40 * ($next_level - 1);
		$this_exp = 10 * pow(($user['level'] -1), 2) + 40 * ($user['level'] - 1);
		$exp_rate = round(100 * ($user['exp'] - $this_exp) / ($next_exp - $this_exp), 2);
		$user['exp_rate'] = $exp_rate > 0 ? $exp_rate: 0;
		$TUser[$uid] = $user;
		return $user;
	}

	/**
	 *根据uid 筛选用户信息
	 *@param array $uids
	 *@return array(nickname,avatar,uid)
	 */
	public function userList($uids = array()) {

		$map = array('id' => array('in', $uids));
		$user = $this->mod->where($map)->field('uid,nickname,avatar')->select();
		if ($user) {
			foreach ($user as $key => &$value) {
				if (empty($value['avatar'])) {
					$value['avatar'] = ''; //C('DOMAIN') . '/Public/Home/images/avatar.jpg';
				} else {
					uri_format($value['avatar']);
				}

			}
		}
		return $user;

	}

	/**
	 * 获取用户详细信息  包括粉丝/关注/订阅游戏量
	 * @author Legend. <xcx_legender@qq.com>
	 * @param
	 * @return
	 */
	// 添加$showUsername参数  liubo 2016-07-11
	public function detail($uid, $showRegTime = false, $field = '', $showUsername = false) {
		$user = $this->info($uid, $showRegTime, $field, $showUsername);
		if ($user) {
			$uid = intval($uid);
			$fEvent = A('Api/Follow', 'Event');
			$user['statis']['follows'] = $fEvent->count_mine($uid);
			$user['statis']['fans'] = $fEvent->count_follows($uid);
			$user['statis']['scribe'] = A('Api/GameScribe', 'Event')->count_mine($uid);
			$user['statis']['zans'] = $this->getLikeCount($uid);
			$user['statis']['videos'] = $this->getVideoCount($uid);

			if (defined('UID')) {
				$user['action']['is_follow'] = $this->follow_status($uid);
			}
		}
		return $user;
	}

	/**
	 * 查询当前用户对该用户的关注状态
	 * @author liubo <liubo@ilongyuan.com.cn>
	 * @param  $uid  需要查询的用户Id
	 * @return   已关注-true，未关注-false
	 */
	public function follow_status($uid) {
		$condition = [
			'uid' => UID,
			'target_id' => $uid
		];
		$followInfo = service('Follow')->where($condition)->find();
		return !empty($followInfo);
	}

	/**
	 *获取用户列表
	 */
	public function lists($filter = array()) {
		$map = array('status' => 1, 'type' => 2);
		if ($filter) {
			$map = array_merge($map, $filter);
		}

		$listData = $this->mod->where($map)->field('uid,nickname')->select();
		return $listData;
	}

	/**
	 *根据昵称查找用户
	 *@return int 存在则返回用户id，反之0
	 */
	public function getUser($nickname) {
//502
		//  nickName => nickname  Legend. 2015-4-21 20:18:54
		$map = array('nickname' => $nickname, 'status' => 1);
		$user = $this->mod->where($map)->find();
		return $user ? $user['id'] : 0;
	}

	/**
	 *注册
	 */
	public function register($data) {
		$result = array('data' => null, 'code' => self::$API_ERROR['SUCCESS'], 'info' => '注册成功'); //返回的结果

		if (empty($data['mobile'])) {
			return $this->returnParamError('手机号不能为空');
		}

		if (empty($data['password'])) {
			return $this->returnParamError('密码不能为空');
		}

		if (empty($data['verify'])) {
			return $this->returnParamError('验证码不能为空');
		}

		if (!checkMobile($data['mobile']) || !checkPassword($data['password'])) {
			return $this->returnParamError('账号或密码格式有误');
		}

		$checkRes = $this->checkSmsVerify($data['verify'], $data['mobile'], $msg);
		if ($checkRes === false) {
			$result['code'] = self::$API_ERROR['VERIFY'];
			$result['info'] = $msg;
			return $result;
		}
 
		if ($data['nickname']) {
			if (!$this->checkNickname($data['nickname'])) {
				$result['code'] = self::$API_ERROR['PARAM'];
				$result['info'] = '昵称不合法';
				return $result;
			}
 		}

		/* 调用注册接口注册用户 */
		$User = service('Users');
		$uid = $User->register($data['mobile'], $data['password'], $data['mobile'], $data['nickname'], ['platform' => $data['platform'] ?: 0]);
		if (0 < $uid) {
			$result['uid'] = $uid;
			$result['score'] = event('Score')->get($uid, 'register');
			event('Statis')->doLocalStatis('register', $data['platform'] ?: 0, 1, ['uid' => $uid]);
			return $result;
		} else {
			$result['code'] = self::$API_ERROR['SERVER'];
			$result['info'] = $User->getError();
			return $result;
		}

	}

	/**
	 *登录
	 *@param $memMdlType string 用户模型所在的项目， 空则当前项目
	 */
	public function login($data, $memMdlType = '') {
		$result = array('data' => null, 'code' => self::$API_ERROR['SUCCESS'], 'info' => '登录成功'); //返回的结果
		if (empty($data['mobile']) || empty($data['password'])) {
		// if (!checkMobile($data['mobile']) || !checkPassword($data['password'])) {
			$result['code'] = self::$API_ERROR['PARAM'];
			$result['info'] = '账号或密码格式有误';
			return $result;
		}

		$user = service('Users');
		$uid = $user->login($data['mobile'], $data['password']); //判断用户是否存在
		switch ($uid) {
		case -1:
			$result['info'] = '用户不存在或被禁用！';
			$result['code'] = self::$API_ERROR['NO_USER'];
			return $result;
			break;
		case -2:
			$result['info'] = '密码错误！';
			$result['code'] = self::$API_ERROR['PASSWORD'];
			return $result;
			break;
		default:
			$result['data'] = array('uid' => $uid);
			event('Score')->get($uid, 'login');
			event('Statis')->doLocalStatis('login', $data['platform'] ?: 0);
			event('Statis')->operLogin($uid, $data['imie'], $data['platform']);
			return $result;
			break;
		}
	}

	


	/**
	 *检测短信验证码
	 */
	public function checkSmsVerify($code, $mobile, &$msg = '') {
		$mdlObj = M('verify');
		$verifyRes = $mdlObj->find($mobile);
		if (empty($verifyRes)) {
			$msg = '请获取验证码';
			return false;
		}

		$verify = $verifyRes['verify'];
		if ($code == $verify) {
			$mdlObj->delete($mobile);
			$expired = $verifyRes['expired'];
			if (time() >= $expired) {
				$msg = '验证码过期';
				return false;
			}
			return true;
		} else {
			$msg = '验证码错误';
			return false;
		}
	}

	/**
	 *重置密码
	 */
	public function resetPwd($uid, $data) {
		$uid = (int)$uid;
		$result = array('data' => null, 'code' => self::$API_ERROR['PARAM'], 'info' => '参数错误'); //返回的结果

		if (empty($data['old'])) {
			$result['info'] = '请输入原密码';
			return $result;
		}
		if (empty($data['password'])) {
			$result['info'] = '请输入新密码';
			return $result;
		}

		$res = service('Users')->updateUserFields($uid, $data['old'], ['password' => $data['password']]);
		if ($res) {
			service('Token')->where( ['uid' => $uid] )->delete();
			$result['code'] = self::$API_ERROR['SUCCESS'];
			$result['info'] = '修改密码成功';
			return $result;
		} else {
			$result['info'] = service('Users')->getError();
			$result['code'] = self::$API_ERROR['SERVER'];
			return $result;
		}
	}

	/**
	 * 重置密码
	 * 忘记密码后的重置密码，不验证旧密码
	 * @author liubo <liubo@ilongyuan.com.cn>
	 */
	public function setPwd($uid, $password) {
		$res = service('Users')->resetPwd($uid, ['password' => $password]);
        if ($res) {
            service('Token')->where(array('uid' => $uid))->save(array('status' => 0));
            return true;
        } else {
            return service('Users')->getError();
        }
	}

	/**
	 *手机验证码的发送
	 *@param array $data 提交的信息
	 *@param boolean $isReg true|注册时发验证码 false|其他情况下的发验证码
	 */
	public function sendSms($data, $isReg = true) {
		if (empty($data['mobile'])) {
			return array('data' => null, 'code' => self::$API_ERROR['PARAM'], 'info' => '参数错误');
		}

		if (false === checkMobile($data['mobile'])) {
			return array('data' => null, 'code' => self::$API_ERROR['MOBILE'], 'info' => '手机号不合法');
		}

		if ($isReg) {
			$result = service('Users')->info($data['mobile'], true);
			if ($result != null) {
				return array('data' => null, 'code' => self::$API_ERROR['MOBILE_EXIST'], 'info' => '手机号已注册');
			}

		}

		$curTime = time();
		$mdlObj = M('verify');
		$verifyRes = $mdlObj->find($data['mobile']);
		//暂时不做验证
		if($result){
			$isExpired = $verifyRes['expired']<$curTime?true:false;
			$isSend = $curTime >($verifyRes['create_time']+60)?true:false;
			if(!$isExpired && $isSend)
				return array('data'=>null, 'code'=>self::$API_ERROR['TOO_TIMES'], 'info'=>'验证码已发送，请等待');
		}

		$smsCode = rand(100000, 999999);

		$body = "您的验证码是：" . $smsCode . "。请不要把验证码泄露给其他人。如非本人操作，可不用理会！";
		$ret = null;
		if (I('is_demo') == 1){
			$res = true;
			$ret['code'] = $smsCode;
		}
		else{
			$res = sendSMS($data['mobile'], $body);
		}
		
		//暂时不等短信发送响应，直接记录
		//if($res === true){
		$expired = $curTime + 60 * 20;
		$addData = array(
			'mobile' => $data['mobile'],
			'verify' => $smsCode,
			'expired' => $expired,
			'create_time' => $curTime,
		);
		if ($verifyRes) {
			$mdlObj->save($addData);
		} else {
			$mdlObj->add($addData);
		}

		// return array('data'=>$smsCode, 'code'=>self::$API_ERROR['SUCCESS'], 'info'=>'验证码发送成功');

		return array('data' => $ret, 'code' => self::$API_ERROR['SUCCESS'], 'info' => '验证码发送成功');
		/* }else{
			             return array('data'=>null, 'code'=>self::$API_ERROR['SERVER'], 'info'=>'验证码发送失败');
		*/

	}

	/**
	 *设置昵称
	 */
	public function setNickname($data) {
		if (false === $this->checkNickname($data['nickname'])) {
			return array('data' => null, 'code' => self::$API_ERROR['NICKNAME__ILLEGAL'], 'info' => '昵称不合法');
		}
		$info = $this->mod->where(array('id' => $data['uid']))->find();
		if (empty($info)) {
			return array('data' => null, 'code' => self::$API_ERROR['NO_USER'], 'info' => '用户不存在');
		}

		if ($info['nickname']) {
			return array('data' => null, 'code' => self::$API_ERROR['NICKNAME'], 'info' => '昵称已设置');
		}

		$save['nickname'] = trim($data['nickname']);
		$save['id'] = (int)$data['uid'];
 		$info = $this->mod->create($save);
		if (!$info) {
			$msg = $this->mod->getError();
			return array('data' => null, 'code' => self::$API_ERROR['NICKNAME__ILLEGAL'], 'info' => $msg);
		}

		if ($this->mod->save($save)) {
			return array('data' => null, 'code' => self::$API_ERROR['SUCCESS'], 'info' => '昵称设置成功');
		} else {
			return array('data' => null, 'code' => self::$API_ERROR['SERVER'], 'info' => '昵称设置失败');
		}

	}

	/**
	 *用户昵称检查是否有敏感词,是否含空格
	 *@param string $nickname
	 *@author susy.liang
	 */
	public function checkNickname($nickname) {
		if (!$nickname || !preg_match('/^[\x7f-\xff\w]+$/', $nickname) || mb_strlen($nickname, 'utf8') > 12) {
			return false;
		}

		if (preg_match('/^游客/', $nickname)) {
			return false;
		}

		$keyWords = A('Api/Sensitive', 'Event')->lists();
		if (!$keyWords) {
			return true;
		}

		foreach ($keyWords['keyword'] as $key => $value) {
			if (strrpos($value, $nickname) !== false) {
				return false;
			}
		}

		return true;
	}

	/**
	 *找回密码
	 */
	public function retrieve($data) {
		$result = array('data' => null, 'code' => self::$API_ERROR['SUCCESS'], 'info' => '找回密码成功');
 
		if (empty($data['mobile'])) {
			return $this->returnParamError('手机号不能为空');
		}

		if (empty($data['password'])) {
			return $this->returnParamError('密码不能为空');
		}

		if (empty($data['verify'])) {
			return $this->returnParamError('验证码不能为空');
		}

		$checkRes = $this->checkSmsVerify($data['verify'], $data['mobile'], $msg);
		if ($checkRes === false) {
			$result['code'] = self::$API_ERROR['VERIFY'];
			$result['info'] = $msg;
			return $result;
		}

		unset($data['verify']);
		$ucenterMem = service('Users');
		$map = array('username' => trim($data['mobile']), 'status' => 1);
		$info = $ucenterMem->where($map)->find();
		if (!$info) {
			$result['code'] = self::$API_ERROR['NO_USER'];
			$result['info'] = '用户不存在';
			return $result;
		}

		$res = service('Users')->resetPwd($info['id'], ['password' => $data['password']]);
		if ($res) {
			service('Token')->where(array('uid' => $data['id']))->save(array('status' => 0));
			$result['code'] = self::$API_ERROR['SUCCESS'];
			$result['info'] = '找回密码成功';
			return $result;
		} else {
			$result['info'] = service('Users')->getError();
			$result['code'] = self::$API_ERROR['SERVER'];
			return $result;
		}

	}

	/**
	 *根据条件进行用户id的获取
	 *@param array $filter 过滤的条件
	 *@return array
	 */
	public function getUid($filter = array(), $base = array('status' => 1)) {
		$map = array_merge((array) $base, (array) $filter);
		return $this->mod->where($map)->getField('id', true);
	}

	/**
	 *设置用户个人资料
	 */
	public function setProfile($uid, $data, &$ret_data) {
		$uid = (int)$uid;
		$res = $this->mod->where(array('id' => $uid))->save($data);
		$user = $this->info($uid);
		$profiles = ['game', 'desc', 'gender', 'age', 'address', 'stars', 'interest', 'turtial'];
		$i = 0;
		foreach ($profiles as $key) {
			if (($key == 'age' && $user[$key] > 0) || $user[$key] != '') {
				$i++;
			}
		}
		if ($i >= 5) {
			// 超过5项设置了 就可以获取积分
			$score = event('Score')->get($uid, 'profile');
			$score != false && $ret_data['score_plus'] = $score;
		}
	}

	######################  FROM 2015-9-22 20:33:26  Legend. ########################

	/**
	 * 获取用户信息的时候 根据发送字段获取更多的信息
	 * @author Legend. <xcx_legender@qq.com>
	 * @param
	 * @return
	 */

	public function getMoreField($uid, $field = '') {
		switch ($field) {
		case 'scribe_game':
			return field_filter(event('GameScribe')->mine($uid, true), ['id', 'name'], true) ?: [];
			break;
		default:
			break;
		}
	}

	/**
	 * 增加用户的经验值
	 * @author Legend. <xcx_legender@qq.com>
	 * @param
	 * @return
	 */

	public function expUP($uid, $exp) {
		$uid = (int)$uid;
		if ($exp <= 0) {
			return;
		}

		$user = $this->info($uid);
		$exp = $user['exp'] + $exp;
		$next_level = $user['level'] + 1;
		//  $exp = 10*level^2 + 40 * level
		$next_exp = 10 * pow(($next_level - 1), 2) + 40 * ($next_level - 1);
		$user['exp'] = $save['exp'] = $exp;
		if ($exp > $next_exp) {
			$user['level'] = $save['level'] = $next_level;
		}
		service('Users')->where(['id' => $uid])->save($save);
	}

	/**
	 * 操作积分
	 * @author Legend. <xcx_legender@qq.com>
	 * @param
	 * @return
	 */

	public function score_oper($uid, $score = 0, $action = '', $memo = '', $target_id = 0, $action_id = 0) {
		// if ($score == 0) return;
		$map = ['id' => (int)$uid];
		$user_score = service('Users')->where($map)->field('score')->find();
		if ($user_score['score'] + $score < 0) {
			return false;
		}
		$res = service('Users')->where($map)->setInc('score', $score);
		$res && service('UserMissionLog')->set_log($uid, $action, $score, $memo, $target_id, $action_id);
		return $res;
	}

	// 设置用户上线
	public function setOnline($uid, $type) {
		$key = "online.user.{$uid}.{$type}";
		$ping = md5(NOW_TIME . rand(100000, 999999));
		$param = [
			'online' => NOW_TIME,
			'ping' => $ping,
			'status' => 0,
		];
		event('Cache')->set($key, $param);
		return $ping;
	}

	public function setOffline($uid, $type, $ping) {
		$key = "online.user.{$uid}.{$type}";
		$data = event('Cache')->get($key);
		if (!$data || $data['ping'] != $ping || $data['status'] == 1) {
			return;
		}

		$online_time = intval((NOW_TIME - $data['online']) / 60) + 1;
		if ($online_time < 0) {
			return;
		}

		$map = [
			'uid' => (int) $uid,
			'type' => $type,
			'date' => date('Y-m-d', NOW_TIME),
		];
		$service = service('ExpDaily');
		$expLog = $service->where($map)->find();
		$max = C('EXP_DAILY_DEVIDE_MAX', null, 30);
		$add = 0;
		if ($expLog) {
			if ($expLog['exp'] < $max) {
				$add = $expLog['exp'] + $online_time < $max ? $online_time : $max - $expLog['exp'];
				$add > 0 && $service->where($map)->setInc('exp', $add);
			}
		} else {
			$add = $online_time < $max ? $online_time : $max;
			$map['exp'] = $add;
			$service->add($map);
		}
		if ($add > 0) {
			$this->expUp($uid, $add);
		}
		event('Cache')->del($key);
	}

	// 设置语音签名
	public function setVoice($uid, $voice_uri, &$ret_data) {
		$data['voice'] = $voice_uri;
		$uid = (int)$uid;
		$res = $this->mod->where(array('id' => $uid))->save($data);
		if ($res) {
			$score = event('Score')->get($uid, 'sound');
			$score != false && $ret_data['score_plus'] = $score;
			$this->user_oper($uid, 'voice', ['uri' => $voice_uri]);
		}
	}

	// 用户操作加入动态
	public function user_oper($uid, $type = 'video', $extends = []) {
		// 发布视频, 语音签名, 配音
		$types = ['video', 'voice', 'dubbing'];
		if (!in_array($type, $types)) {
			return;
		}

		$data = [
			'uid' => $uid,
			'type' => $type,
		];
		$extends && $data['extends'] = $extends;
		switch ($type) {
		case 'voice':
			$data['memo'] = "更新了语音签名";
			break;
		case 'dubbing':
			$data['memo'] = "给视频配音了";
			break;
		default:
			$data['memo'] = "发布了一条视频";
			break;
		}
		$serv = service('UserOper');
		$data = $serv->create($data);
		$data && $serv->add($data);
		return true;
	}

	// 用户分享接口
	public function share($uid, $type = '', $id = 0) {
		$res = event('Score')->get($uid, 'share');
		if (!empty($type)) {
			switch ($type) {
				case 'video':
					if (!empty($id)) {
						service('video')->where(['id' => $id])->setInc('share', 1);
					}
					break;
				default:
					break;
			}
		}
		return $res;
	}

	// 根据推广获取用户信息
	public function getByRefer($referral) {
		if (!$referral) {
			return null;
		}

		return service('Users')->where(['referral' => $referral])->find();
	}

	// 获取用户的被点赞总数
	public function getLikeCount($uid) {
		$map['target_id'] = intval($uid);
		$map['type'] = 2;
		return service('Like')->where($map)->count() ?: 0;
	}


	################## FROM 2016-03-14 20:55:16 Legend.Xie ##############

	public function ssoLogin($channel, $openid, $nickname, $extend = [], $open = [])
	{
		if (!$openid){
			return false;
		}
		$model = service('Sso');
		$uid = $model->getUid($channel, $openid);
		if (!$uid){
			$username = $channel.'_'.$openid;
			$password = rand(100000000, 999999999);

			// 添加sso_channel字段 liubo 2016-7-15 11:22:26
			$extend['sso_channel'] = $channel;

			$uid = $this->mod->register($username, $password, '', '', $extend);
			if ($uid > 0){
				// 添加STARGAME认证标识
				// by liubo<liubo@ilongyuan.com.cn> at 2016-07-07
				if ($channel == 'stargame') {
					$icon = service('Icon')->where(['name' => 'NGA玩家'])->find();
					if ($icon) {
						$auth_data = [
							'id' => (int)$uid,
							'auth_icon' => (int)$icon['id'],
						];
						service('Users')->save($auth_data);
					}
				}
				event('Score')->get($uid, 'register');
				event('Statis')->doLocalStatis('register', $extend['platform'] ?: 0, 1, ['uid' => $uid, 'from' => $channel]);
			}
			if ($uid < 0){
				$user = $this->mod->info($username, true);
				if ($user){
					$uid = $user['id'];
				}
			}

			if ($uid) {
				$model->addNew($channel, $openid, $uid, $open);
			} else {
				return false;
			}
		} else {
			event('Score')->get($uid, 'login');
		}
 		return $uid;
	}

	/**
	 *用户token的生成
	 *@param int $uid 用户ID
	 *@param string $imie 手机设备码
	 */
	public function genToken($uid, $imie, $score = false, $platform = 0) {
		$tokenMdl = service('Token');
		$res = $tokenMdl->addToken($uid, $imie, $appToken, $platform);
		if ($res == true) {
			$data = array('appToken' => $appToken);

			// $nickname = get_nickname($uid);
			// $data['nickname'] = $nickname?$nickname:null;
			// $avatar = get_avatar($uid);

			$user = $this->info($uid);
			$nickname = $user['nickname'];
			$data['nickname'] = $nickname ? $nickname : null;
			$avatar = $user['avatar'];
			$data['referral'] = $user['referral'];
			$data['background'] = $user['background'];
			$data['turtial'] = $user['turtial'];
			$data['level'] = $user['level'];

			$data['auth'] = $user['auth'];

			// uri_format($avatar);
			$data['avatar'] = $avatar; //?$avatar:C('DOMAIN').'/Public/Home/images/avatar.jpg';
			$data['id'] = $uid;
			$score != false && $data['score_plus'] = $score;
			if ($nickname) {
				return array('data' => $data, 'code' => self::$API_ERROR['SUCCESS'], 'info' => '登录成功');
			} else {
				return array('data' => $data, 'code' => self::$API_ERROR['NO_NICKNAME'], 'info' => '请设置昵称');
			}
		} else {
			$errmsg = $tokenMdl->getError();
			return array('data' => null, 'code' => self::$API_ERROR['TOKEN_FAILED'], 'info' => $errmsg);
		}
	}

	/**
	 * 查询用户的视频数量
	 * @author liubo <liubo@ilongyuan.com.cn>
	 */
	public function getVideoCount($uid) {
		return service('video')->where(['uid' => $uid])->count();
	}

	public function isMatchGroup($uid) {
		// return D('AuthGroupAccess')->where(['uid' => $uid, 'group_id' => self::MATCH_USER_GROUP])->count() > 0;
		$user = $this->mod->find($uid);
		return !empty($user['groups']) && in_array('match', $user['groups']);
	}
}
